﻿@model TopicModel

@using SmartStore.Web.Models.Topics;

@{
    var isPopup = ViewBag.IsPopup;
    if (isPopup == null || isPopup == false)
    {
        Layout = "_Layout";
    }
    else
    {
        Layout = "_Layout.Popup.cshtml";
    }

    if (!Model.IsPasswordProtected)
    {
        Html.AddTitleParts(!String.IsNullOrEmpty(Model.MetaTitle) ? Model.MetaTitle : Model.Title);
        Html.AddMetaDescriptionParts(Model.MetaDescription);
        Html.AddMetaKeywordParts(Model.MetaKeywords);
    }

    if (Model.HtmlId.HasValue())
    {
        Html.SetHtmlId(Model.HtmlId);
    }

    if (Model.BodyCssClass.HasValue())
    {
        Html.AddBodyCssClass(Model.BodyCssClass);
    }
}

@if (Model.IsPasswordProtected)
{
	// For popup topics, use a minimal layout that includes the Ajax and jQuery scripts
	if (isPopup != null && isPopup == true)
	{
		Layout = "_RootPopup";
	}
    <script>
        $(document).ready(function () {
            $('#ph-topic').hide();
            $('#ph-password #password').select().focus();
		});

        function OnAuthenticateSuccess(context) {
            if (context.Authenticated) {
                $('#ph-title .page-title h1').html(context.Title);
                if ($('#ph-title .page-title h1').text().length == 0) {
                    $('#ph-title').hide();
                }
                $('#ph-topic .page-body').html(context.Body);
                $('#ph-password').hide();
                $('#ph-topic').show();
            }
            else {
                $('#password-error').text(context.Error);
                $('#ph-password #password').select().focus();
            }
        }
    </script>

    <div id="ph-password">
        @using (Ajax.BeginForm("Authenticate", new AjaxOptions
		{
			HttpMethod = "Post",
			OnSuccess = "OnAuthenticateSuccess",
			LoadingElementId = "authenticate-progress"
		}))
		{
            @Html.HiddenFor(model => model.Id)
            <div class="enter-password-title">
                @T("Topic.EnterPassword")
            </div>
            <div class="enter-password-form">
                @Html.Password("password")
                <input type="submit" value="@T("Topic.Button")" class="button-1 topic-password-button"/>
                <span id="authenticate-progress" style="display: none;">@T("Common.Wait...")
                    <img src='@Url.Content("~/Content/Images/ajax_loader_small.gif")' alt="@T("Common.Wait...")" />
                </span>
            </div>
            <div class="password-error">
                <span id="password-error"></span>
            </div>
		}
    </div>
}

@if (Model.Title.HasValue() || Model.Intro.HasValue() || Model.Body.HasValue())
{
	<div class="page topic-page" id="ph-topic">
		@if (Model.Title.HasValue())
		{
			<div id="ph-title">
				<div class="page-title">
					@Html.Raw("<" + Model.TitleTag + Html.LanguageAttributes(Model.Title) + " class=\"block-title\">")
					@Html.Raw(Model.Title)
					@Html.Raw("</" + Model.TitleTag + ">")
				</div>
			</div>
		}

		@if (Model.Intro.HasValue())
		{
			<p class="ph-intro lead" @Html.LanguageAttributes(Model.Intro)>
				@Html.Raw(Model.Intro)
			</p>
		}

		@{ Html.RenderWidget("topic_body_before"); }

		@if (Model.Body.HasValue())
		{
			<div class="page-body html-editor-content" @Html.LanguageAttributes(Model.Body)>
				@Html.Raw(Model.Body)
			</div>
		}

		@{ Html.RenderWidget("topic_body_after"); }
	</div>
}
